const setTimer = (el, binding) => {
    let loading = el.querySelector('.el-icon-loading');
    let NProgress = $('#nprogress');
    let timeout = 500, showLoading = true;
    if (typeof binding.value === 'number') {
        timeout = binding.value;
    }
    if (typeof binding.value === 'object') {
        showLoading = binding.value.loading;
        if (binding.value.timeout) {
            timeout = binding.value.timeout;
        }
    }
    if (NProgress.length > 0) {
        if (!el.disabled) {
            el.disabled = true;
        }
        if (!$(el).hasClass('is-loading')) {
            el.classList.add('is-loading');
        }
        if (showLoading && !loading) {
            $(el).prepend(`<i class="el-icon-loading" style="margin-right: 5px;" />`);
        }
        setTimeout(() => {
            setTimer(el, binding);
        }, timeout);
    } else {
        el.disabled = false;
        el.classList.remove('is-loading');
        if (showLoading && loading) {
            loading.remove();
        }
    }
};

const clickEvent = () => {

}
export default {
    inserted(el, binding, vNode) {
        el.removeEventListener('click', () => {
            setTimer(el, binding);
        });
        if (binding.value === false) {
            return;
        }
        el.addEventListener('click', () => {
            setTimer(el, binding);
        });
    },
};
